package com.service.impl;

import com.dto.FriendsCommentDto;
import com.dto.FriendsDto;
import com.github.pagehelper.PageHelper;
import com.mapper.FriendsMapper;
import com.pojo.Friends;
import com.service.FriendsService;
import com.utils.PageResult;
import com.utils.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FriendsServiceImpl implements FriendsService {

    @Autowired
    private FriendsMapper friendsMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Override
    public void submit(FriendsDto friendsDto) {
        Friends friends = new Friends();
        BeanUtils.copyProperties(friendsDto, friends);
        friends.setFriendId(snowFlake.nextId());
        friends.setCreateTime(LocalDateTime.now().toString());
        friendsMapper.save(friends);
    }

    @Override
    public PageResult pageQuery(int page, int pageSize) {
        List<Friends> friends = friendsMapper.pageQuery((page - 1) * 10, pageSize);
        for (Friends f : friends) {
            List<FriendsCommentDto> comments = friendsMapper.selectcomment(f.getFriendId());
            for(FriendsCommentDto c:comments){
                c.setName(friendsMapper.selectNameByPhone(c.getPhone()));
            }
            f.setComments(comments);
        }
        int total = friendsMapper.getTotal();
        return new PageResult(total, friends);
    }

    @Override
    public void nice(Long friendId) {
        friendsMapper.addcount(friendId);
    }

    @Override
    public void comment(FriendsCommentDto friendsCommentDto) {
        friendsCommentDto.setCreateTime(LocalDateTime.now().toString());
        friendsMapper.savecomment(friendsCommentDto);
    }
}
